package com.zcf.gameController.gameUtils;

import com.zcf.gameController.F_Websocket_s;
import com.zcf.gameController.WebSocketBull;
import com.zcf.pojo.Msg;
import com.zcf.pojo.RoomsParam;
import com.zcf.pojo.UserBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jj
 * @date 2019/11/26 11:41
 */
public class UserThread extends  Thread {

    @Override
    public  void run() {
        System.out.println("---------------------开启用户检测状态线程--------------------------");
        while (true ) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for ( RoomsParam myRoom: F_Websocket_s.rooms.values()){
                try {
                    if (myRoom.getIsStart()==0) {
                        if (myRoom.getUsers().values().size() > 2) {
                            List<UserBean> userBeans =myRoom.getUsers().values().stream().filter(userBeanM->userBeanM.getType().equals(1)).collect(Collectors.toList());
                            Map<Integer, List<UserBean>> collect = userBeans.stream().collect(Collectors.groupingBy(UserBean::getIsReady));
                            if (collect.get(1)==null){
                                continue;
                            }
                            if (collect.get(0)==null){
                                continue;
                            }
                            if (userBeans.size()!=collect.get(1).size()&&collect.get(1).size()>1){
                                List<UserBean> userList=collect.get(0);
                                for (UserBean u:userList){
                                    if (u.getIsThread() == 0) {
                                        continue;
                                    }
                                    u.setIsThread(u.getIsThread() - 1);
                                    System.out.println("起身倒计时:"+u.getIsThread());
                                    if (u.getIsThread() <= 0) {
                                        if (u.getIsReady()==0){
                                            myRoom.getLock().lock();
                                            try {
                                                if (myRoom.getPostions().contains(u.getId().intValue())){
                                                    int i1 = myRoom.getPostions().indexOf(u.getId().intValue());
                                                    myRoom.getPostions().set(i1,0);
                                                }
                                            }catch (Exception e){
                                                System.out.println(e.getMessage());
                                            }finally {
                                                myRoom.getLock().unlock();
                                            }
                                            u.setType(0);
                                            u.setIsThread(30);
                                            u.setPosition(-1);
                                            Map<String, Object> data = new HashMap<>();
                                            data.put("userid", u.getId());
                                            Msg msg = new Msg(200, "30秒内未准备！起身", null, "sitUp");
                                            F_Websocket_s.clients.get(u.getId() + "").broadcastMe(msg, u.getSession(), u);
                                            Msg msgs = new Msg(200, "用户" + u.getWxname() + "30秒内未准备！", null, "sitUp");
                                            F_Websocket_s.clients.get(u.getId() + "").broadcastB(msgs, u.getSession().getId(), myRoom);
                                            if (myRoom.getIsStart().equals(0)) {
                                                List<UserBean> ulist =myRoom.getUsers().values().stream().filter(userBeanM->userBeanM.getType().equals(1)).collect(Collectors.toList());
                                                F_Websocket_s.clients.get(u.getId() + "").chatRoom(userBeans, collect);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
            for ( RoomsParam myRoom: WebSocketBull.rooms.values()){
                try {
                    if (myRoom.getIsStart()==0) {
                        if (myRoom.getUsers().values().size() > 2) {
                            List<UserBean> userBeans =myRoom.getUsers().values().stream().filter(userBeanM->userBeanM.getType().equals(1)).collect(Collectors.toList());
                            Map<Integer, List<UserBean>> collect = userBeans.stream().collect(Collectors.groupingBy(UserBean::getIsReady));
                            if (collect.get(1)==null){
                                continue;
                            }
                            if (collect.get(0)==null){
                                continue;
                            }
                            if (userBeans.size()!=collect.get(1).size()&&collect.get(1).size()>1){
                                List<UserBean> userList=collect.get(0);
                                for (UserBean u:userList){
                                    if (u.getIsThread() == 0) {
                                        continue;
                                    }
                                    u.setIsThread(u.getIsThread() - 1);
                                    System.out.println("起身倒计时:"+u.getIsThread());
                                    if (u.getIsThread() <= 0) {
                                        if (u.getIsReady()==0){
                                            myRoom.getLock().lock();
                                            try {
                                                if (myRoom.getPostions().contains(u.getId().intValue())){
                                                    int i1 = myRoom.getPostions().indexOf(u.getId().intValue());
                                                    myRoom.getPostions().set(i1,0);
                                                }
                                            }catch (Exception e){
                                                System.out.println(e.getMessage());
                                            }finally {
                                                myRoom.getLock().unlock();
                                            }
                                            u.setPosition(-1);
                                            u.setType(0);
                                            u.setIsThread(30);
                                            Map<String, Object> data = new HashMap<>();
                                            data.put("userid", u.getId());
                                            Msg msg = new Msg(200, "30秒内未准备！起身", null, "sitUp");
                                            WebSocketBull.clients.get(u.getId() + "").broadcastMe(msg, u.getSession(), u);
                                            Msg msgs = new Msg(200, "用户" + u.getWxname() + "30秒内未准备！", null, "sitUp");
                                            WebSocketBull.clients.get(u.getId() + "").broadcastB(msgs, u.getSession().getId(), myRoom);
                                            if (myRoom.getIsStart().equals(0)) {
                                                System.out.println(collect);
                                                System.out.println(userBeans);
                                                List<UserBean> ulist =myRoom.getUsers().values().stream().filter(userBeanM->userBeanM.getType().equals(1)).collect(Collectors.toList());
                                                WebSocketBull.clients.get(u.getId() + "").chatRoom(ulist, collect);
                                            }

                                        }
                                    }
                                }
                            }
                        }
                    }
                }catch (Exception e1){
                    e1.printStackTrace();
                }

            }
        }
    }

}
